package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;
    @Autowired
    private JedisPool jedisPool;

    //获取详细地址,主要是经纬度,用于创建地图,显示公司分布
    @GetMapping("/getMap")
    public Result getAddresses(){
        try{
            List<Address> addressList = addressService.getAddresses();
//            System.out.println("控制层~~~~"+addressList);
            return new Result(true,MessageConstant.GET_MAP_SUCCESS,addressList);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MAP_FAILED);
        }
    }

    //上传头像
    @PostMapping("/uploadImg")
    public Result uploadImg(MultipartFile imgFile) {
        try {
            //获取原始文件名称
            String fileName = imgFile.getOriginalFilename();
            //截取文件扩展名
            String extension = fileName.substring(fileName.lastIndexOf(".") - 1);
            //构建新文件名称
            fileName = UUID.randomUUID().toString() + extension;

            //执行文件上传
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);

            //保存图片名称到redis数据库
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);

            return new Result(true, MessageConstant.UPLOAD_SUCCESS, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    //添加地址
    @PostMapping("/addAddr")
    public Result addAddr(@RequestBody Address address){
        try{
            addressService.addAddr(address);
            return new Result(true,MessageConstant.ADD_ADDR_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ADDR_FAILED);
        }
    }

    //根据id删除
    @PreAuthorize("hasAuthority('ADDR_DELETE')")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        try{
            addressService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_ADDR_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ADDR_FAILED);
        }
    }

    //修改地址
    @PutMapping("/update")
    public Result update(@RequestBody Address address){
        try{
            addressService.update(address);
            return new Result(true,MessageConstant.UPDATE_ADDR_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_ADDR_FAILED);
        }
    }

    //根据id查询
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id){
        try{
            Address address = addressService.findById(id);
            return new Result(true,MessageConstant.QUERY_ADDR_SUCCESS,address);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ADDR_FAILED);
        }
    };

    //分页查询
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        try{
            PageInfo pageInfo = addressService.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_ADDR_SUCCESS,pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ADDR_SUCCESS);
        }
    }

}
